package fe.pong.tennis;

/**
 * ...
 * @author Mauricio
 */
class Score
{
	private var home:Int;
	private var guest:Int;
	private var homeGame:Int;
	private var guestGame:Int;
	private var homeSet:Int;
	private var guestSet:Int;
	private var advantageG: Bool;
	private var advantageH: Bool;
	private var state: String;
	
	public function new() 
	{
		home = 0;
		guest = 0;
		homeGame = 0;
		guestGame = 0;	
		homeSet = 0;
		guestSet = 0;	
		advantageG = false;
		advantageH = false;
		state = "play";
	}
	
	public function addHomePoint() {
		if (advantageH){
			home = 0;
			guest = 0;
			homeGame = homeGame + 1;
			advantageH = false;
			}
		else 
			if (advantageG)
				home = 40;
        
		if (home == 0)
			home = 15;
		else
			if (home == 15)
				home = 30;
			else
				if (home == 30)
					home = 40;
				else
					if (home == 40 && guest == 40)
						advantageH = true;
					else{ 
						home = 0;
						guest = 0;
						homeGame = homeGame + 1;						
						}					
		if(homeGame == 6 ){
			homeSet = homeSet + 1;
			homeGame = 0;
			guestGame = 0;
		}
	}
	
	public function addGuestPoint() {
		if (advantageG){
			guest = 0;
			home = 0;
			guestGame = guestGame + 1;
			advantageG = false;
			}
		else 
			if (advantageH)
				guest = 40;
				
		if (guest == 0)
			guest = 15;
		else
			if (guest == 15)
				guest = 30;
			else
				if (guest == 30)
					guest = 40;
				else
					if (guest == 40 && home == 40)
						advantageG = true;
					else{
						guest = 0;
						home = 0;
						guestGame = guestGame + 1;						
					}				
		
		if (guestGame == 6 ){
			guestSet = guestSet + 1;
			guestGame = 0;
			homeGame = 0;
		}
	}
	
	public function getState(): String {
		return 	state;
	}
	
	public function getWinner(set : Int): Bool {
		if (guestSet == set){
			state = 'guest win';
			return true;
		}
		else
			if (homeSet == set){
				state = 'home win';
				return true;
			}
			return false;
	}
	
	public function gethome(): Int {				
		return this.home;
	}
	
	public function getGuest(): Int {		
		return this.home;
	}
	
	public function gethomeGame(): Int {		
		return this.homeGame;
	}
	
	public function getGuestGame(): Int {		
		return this.guestGame;
	}
	
	public function getHomeSet(): Int {		
		return this.homeSet;
	}
	
	public function getGuestSet(): Int {		
		return this.guestSet;
	}
	
	public function getHomeResult(): String {
		var aux : String;
		aux = "";
		if (advantageH)
			aux = "Adv";
		else			
			aux = this.home+"";
		if (this.homeGame != 0 || this.homeSet != 0)
			aux = aux + "  " +this.homeGame;
		if (this.homeSet != 0)
			aux = aux + "  " +this.homeSet;
		//return (this.home+ " "+ this.homeGame + " "+ this.homeSet);
		return aux;
	}
	
	public function getGuestResult(): String {
		var aux : String;
		aux = "";
		if (advantageG)
			aux = "Adv";
		else
			aux = this.guest + "";
		if (this.guestGame != 0 || this.guestSet != 0)
			aux = aux + "  " +this.guestGame;
		if (this.guestSet != 0)
			aux = aux + "  " +this.guestSet;
		//return (this.guest+ " "+ this.guestGame + " "+ this.guestSet);
		return aux;
	}
	
}